Native malloc fixes and cleanup#550
Conversation
|
CI Test ResultsRun: #26542176878 | Commit:
Status Overview
Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled Summary: Total: 32 | Passed: 32 | Failed: 0 Updated: 2026-05-27 22:37:23 UTC |
…cking Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
rkennke
left a comment
There was a problem hiding this comment.
Looks good. I assume the added overhead of the CS in maybeRecord() is acceptable? E.g. malloc-heavy workloads?
I would lean towards 'yes' - but I don't have any benchmarks for that :( |
What does this PR do?:
Fixes reentrancy double-counting in the native malloc tracer, cleans up hook dispatch for musl, and removes dead code.
CriticalSectionguard inmaybeRecordto prevent profiler-internal allocations (e.g. JFR buffer growth) from re-entering the hook and being double-countedcalloc_hook_dummy/posix_memalign_hook_dummypass-through hooks with a simple skip: on musl wherecallocdelegates tomallocandposix_memaligndelegates toaligned_allocinternally, we leave those GOT entries unpatched and let the inner hook record the allocation_orig_free/SAVE_IMPORT(free)— free is not tracked for liveness; the resolved pointer was only used for a probe cleanup where plainfree()is correct_calloc_hook_fn/_posix_memalign_hook_fnpre-computed pointer members and theNO_OPTIMIZEmacro that existed solely for the dummy hooksMallocTracer::installHooks()inLibraries::refresh()onMallocTracer::running()to avoid unnecessary GOT patching work between profiling sessionsMotivation:
Profiler-internal allocations triggered inside
recordMalloc(e.g. sample buffer allocation) re-enter the patched GOT and were being silently double-counted. The dummy hook approach for musl added function-call overhead on everycalloc/posix_memalignwithout benefit.Additional Notes:
detectNestedMalloc()is kept — it still drives the decision of whether to skip patchingcalloc/posix_memalignon musl.How to test the change?:
Covered by existing integration tests:
NativememProfilerTest— verifiesprofiler.MallocJFR events have positive size, non-zero address, and Java stack traceNativememSampledProfilerTest— verifies weight and sample count from repeated allocationsFor Datadog employees:
Unsure? Have a question? Request a review!